package com.youdianstar.app.ui.account

import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.network.BaseApiResultObserver
import com.youdianstar.app.BR
import com.youdianstar.app.R
import com.youdianstar.app.databinding.ActivityCancelBinding
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.HTTP_BRS_URL
import com.youdianstar.app.common.Language
import com.youdianstar.app.ui.web.WebActivity
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.Tool
import com.youdianstar.app.common.auko.startActivity
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.Locale


/**
 * File: CancelActivity
 * Author: 82149 Create: 2023/8/7 12:21
 * Changes (from 2023/8/7)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class CancelActivity :BaseActivity<ActivityCancelBinding,LoginViewModel>(){

    override fun layoutId(): Int = R.layout.activity_cancel

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): LoginViewModel {
        val model: LoginViewModel by viewModel()
        return model
    }

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this, getString(R.string.Cancellation_Title))

        Tool.setUnderLine(this, mBinding.tvAgreementContent, getString(R.string.Cancellation_agreementText), R.color.text_default)
    }

    override fun initLiveObserver() {
        super.initLiveObserver()
        mViewModel.cancelResultLiveData.observe(this, object : BaseApiResultObserver<Any>(canEmpty = true){
            override fun onDataResult(data: Any?) {
                super.onDataResult(data)
                dismissDialog()

                DataAppManager.instance.clearUserWithCookies()
                startActivity<LoginActivity>()
                finish()
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e)
            }
        })
    }

    override fun initClickEvent() {
        mBinding.btnConfirm.setThrottleListener {
            if (!mBinding.boxAgree.isChecked){
                toastShow(getString(R.string.Cancellation_agreementRequest))
                return@setThrottleListener
            }
            mViewModel.cancelUserAccount()
        }

        mBinding.tvAgreementContent.setThrottleListener {
            val langParam = getAgreementLangParam()
            val termsUrl = "${HTTP_BRS_URL}/sw/app/agreement/user.html?lang=$langParam"
            startActivity<WebActivity>(
                Extra.IT_DATA to termsUrl,
                Extra.IT_NOTIFY to getString(R.string.login_termOfUse)
            )
        }
    }

    /**
     * 获取协议语言参数
     * 规则：中文(zh) -> cn，其他语言 -> vi（默认）
     */
    private fun getAgreementLangParam(): String {
        val languageCode = LanguageConfig.instance.getLanguageCode()
        return if (languageCode == Language.ZH) "cn" else "vi"
    }

}